package com.chs.recoginition.controller;


import com.chs.recoginition.core.utils.LocalDateTimeUtils;
import com.chs.recoginition.core.utils.UploadUtil;
import com.chs.recoginition.pojo.IdentificationContent;
import com.chs.recoginition.service.IIdentificationContentService;
import com.chs.recoginition.service.ImageRecognitionService;
import com.chs.recoginition.vo.PageRespVo;
import com.chs.recoginition.vo.Result;
import com.chs.recoginition.vo.reqvo.IdentificationPageVo;
import com.chs.recoginition.vo.respVo.ImageRecognitionRespVo;
import com.chs.recoginition.vo.respVo.UserRespVo;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 识别内容 前端控制器
 * </p>
 *
 * @author CHS
 * @since 2023-04-08
 */
@RestController
@RequestMapping("/identificationContent")
public class IdentificationContentController extends BaseController {
    @Resource
    private IIdentificationContentService identificationContent;
    @Resource
    private ImageRecognitionService imageRecognitionService;

    /**
     * 分页查询
     *
     * @param reqVo
     * @return
     * @throws IOException
     */
    @PostMapping("/findPage")
    Result<PageRespVo<ImageRecognitionRespVo>> findPage(@RequestBody IdentificationPageVo reqVo) {
        return Result.success(identificationContent.findPage(reqVo));
    }


    @PostMapping("/findListByParentId")
    Result<List<IdentificationContent>> findListByParentId(@RequestBody HashMap<String, String> data) {
        String parentId = data.getOrDefault("parentId", null);
        if (StringUtils.isBlank(parentId)) {
            return Result.success();
        }
        return Result.success(identificationContent.findListByParentId(parentId));
    }

    /**
     * 新增
     *
     * @param reqVo
     * @return
     */
    @PostMapping("/save")
    Result<Void> save(@RequestBody ImageRecognitionRespVo reqVo) {
        UserRespVo user = getUser();
        identificationContent.save(reqVo, user);
        return Result.success();
    }

    /**
     * 新增
     *
     * @param reqVo
     * @return
     */
    @PostMapping("/saveInfo")
    Result<Void> saveInfo(@RequestBody IdentificationContent reqVo) {
        UserRespVo user = new UserRespVo();
        if(reqVo.getParentId() != null) {
            user = getUser();
        }
        identificationContent.saveInfo(reqVo, user);

        return Result.success();
    }

    /**
     * 修改
     *
     * @param vo
     * @return
     */
    @PostMapping("/modify")
    Result<Void> modify(@RequestBody IdentificationContent vo) {
        identificationContent.modify(vo);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param reqVo
     * @return
     */
    @PostMapping("/remove")
    Result<Void> remove(@RequestBody String reqVo) {
        identificationContent.remove(reqVo);
        return Result.success();
    }


    /**
     * 上传图片
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    private Result<String> uploadUserHeader(HttpServletResponse response, MultipartFile file, Integer type) throws IOException {
        String fileName = UploadUtil.uploadPicture(file, "auto", "baidu");
        String result = UploadUtil.localhost + "/imageRecognition/" + fileName;
        return Result.success(result);
    }


    /**
     * 上传图片
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/identify", method = RequestMethod.POST)
    private Result<Void> identify(HttpServletResponse response, MultipartFile file, String type) throws IOException {
        UserRespVo user = getUser();
        File fileImage = new File(Objects.requireNonNull(file.getOriginalFilename()));
        FileUtils.copyInputStreamToFile(file.getInputStream(), fileImage);
        String baseStr = null;
        try {
            //file转base64
            FileInputStream inputStream = new FileInputStream(fileImage);
            byte[] buffer = new byte[(int) fileImage.length()];
            inputStream.read(buffer);
            inputStream.close();
            baseStr = new BASE64Encoder().encode(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //删除临时文件
        if (fileImage.exists()) {
            fileImage.delete();
        }
        assert baseStr != null;
        baseStr = baseStr.replaceAll("\r\n", "");
//        System.out.println(baseStr);
        ImageRecognitionRespVo respVo = new ImageRecognitionRespVo();
        if("1".equals(type)) {
            respVo = imageRecognitionService.getAdvancedGeneral(baseStr);
        }
        if("2".equals(type)) {
            respVo = imageRecognitionService.getAnimal(baseStr);
        }
        if("3".equals(type)) {
            respVo = imageRecognitionService.getPlant(baseStr);
        }
        if("4".equals(type)) {
            respVo = imageRecognitionService.getLogo(baseStr);
        }
        if("5".equals(type)) {
            respVo = imageRecognitionService.getIngredient(baseStr);
        }
        if("6".equals(type)) {
            respVo = imageRecognitionService.getDish(baseStr);
        }
        if("7".equals(type)) {
            respVo = imageRecognitionService.getRedWine(baseStr);
        }
        if("8".equals(type)) {
            respVo = imageRecognitionService.getCurrency(baseStr);
        }
        if("9".equals(type)) {
            respVo = imageRecognitionService.getLandmark(baseStr);
        }
        if(respVo != null) {
            identificationContent.save(respVo,user);
        }
        System.out.println(respVo);
        return Result.success();
    }
}
